<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';

// 
class shipping_provider_2 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {
            $tracking_number = str_replace('A', '', $this->_shipment ['tracking_number']);

            $str_link = $this->_shipment ['url'] !== '' ? $this->_shipment ['url'] : "http://etrace.9625taqbin.com/gli_trace/GDXTX010S10Action_doSearch.action?jvCd=42dec70a4f25166d&sTimeDifference=671cb6bcdedd72c7&sSelectedLanguage=73f9a3a3f2cf56bc&sCountryCd=0968983fa1feb763&sLanguageMode=0&CHAR_SET=c4f39f2062f3cd6b792c5d489598e5e224481782bd2fdaba&sDefCharSet=3f572693955bb3ff&sCharSetCsv=3f572693955bb3ff&action%3AGDXTX010S10Action_doSearch=Track&tTrackingNoInputVal1={$tracking_number}&tTrackingNoInputVal2=&tTrackingNoInputVal3=&tTrackingNoInputVal4=&tTrackingNoInputVal5=&tTrackingNoInputVal6=&tTrackingNoInputVal7=&tTrackingNoInputVal8=&tTrackingNoInputVal9=&tTrackingNoInputVal10=";
            $html = file_get_html($str_link);

            $status_list = array();

            foreach ($html->find('table.meisai tbody tr') as $tr) {
                $tds = $tr->find('td');
                $status_e = array_shift($tds);

                $month_date_e = array_shift($tds);
                preg_match("/(\d{2})-(\d{2})*/", $month_date_e->plaintext, $matches_m);
                list($date_m, $j, $m ) = $matches_m;

                $hour_e = array_shift($tds);
                preg_match("/(\d{2}):(\d{2})*/", $hour_e->plaintext, $matches_h);
                list($time_m, $H, $i ) = $matches_h;

                $status_list [] = array(
                    'time' => "{$m}-{$j} {$H}:{$i}:00",
                    'status' => "({$this->_shipment ['shipment_provider_name']}) " . preg_replace('!\s+!smi', '', strip_tags($status_e->plaintext))
                );
            }
            return array('sortable' => FALSE, 'status_list' => array_reverse($status_list));
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
